/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rgenerator;

import java.io.File;
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author alex2stf <alex2stf@yahoo.com>
 */
public class SettingsRead {
    
    private Document continut;
    public SettingsRead(){}
    
    
    public SerialObject read(File file, String saveLoc, String loadLoc, SerialObject lastSaved){
        System.out.print("reading file "+file.getName());
        SerialObject obiectDeReturnat = lastSaved;
        
         try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            continut=doc;
            //System.out.println("Root element " + doc.getDocumentElement().getNodeName());
            
            //NodeList nodeLst = doc.getElementsByTagName("classfilersetting");
           
           //nodelist returneaza array cu noduri
            NodeList nodeLst = doc.getChildNodes().item(0).getChildNodes();
            System.out.println(nodeLst.getLength());
            
             for (int i = 0; i < nodeLst.getLength(); i++) {
                Node an=nodeLst.item(i);
                 if(an.getNodeType()==Node.ELEMENT_NODE) {
                     System.out.println(i+" <<<"+an.getNodeName()+" >>> "+an.getTextContent());
                 }
             }
             
             String packName=nodeLst.item(1).getTextContent();
             String mainClass=nodeLst.item(3).getTextContent();
             String inputFolder=nodeLst.item(5).getTextContent();
             String outpufolder=nodeLst.item(7).getTextContent();
             
             boolean getterMode=(nodeLst.item(9).getTextContent().equals("true"))?true:false;
             boolean slashInfront=(nodeLst.item(11).getTextContent().equals("true"))?true:false;
             boolean backSlash=(nodeLst.item(13).getTextContent().equals("true"))?true:false;
             boolean doubleSlash=(nodeLst.item(15).getTextContent().equals("true"))?true:false;
             System.out.println(packName+"||"+mainClass);
             
            obiectDeReturnat =new SerialObject(packName, mainClass, inputFolder, outpufolder, getterMode, doubleSlash, slashInfront, backSlash, saveLoc, loadLoc);
        
            
  
    } catch (Exception e) {
        
    }
        
         
        return obiectDeReturnat;
    }//exit constructor
}
